package org.csu.small.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.csu.small.common.Constant;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.User;
import org.csu.small.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author Rambo
 * @date 2020-07-07 23:03
 */
@Controller
@RequestMapping("/manage/user/")
@Api(value = "API - AUserController", description = "管理员用户模块接口详情")
public class AUserController {
    @Autowired
    private IUserService userService;

    @PostMapping("login")
    @ResponseBody
    @ApiOperation(value = "登录")
    public UniformResponse<User> login(String loginString, String password, HttpSession session){
        UniformResponse<User> response = userService.manageLogin(loginString,password);
        if(response.isSuccess()){
            session.setAttribute(Constant.LOGIN_USER, response.getData());
        }
        //生成令牌，用Guawacache
        return response;
    }

    @GetMapping("list")
    @ResponseBody
    @ApiOperation(value = "后台查询用户")
    public UniformResponse<Object> getUserList(
            @RequestParam(name = "keyword", required = false) String keyword,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return userService.getUserList(keyword, pageNum, pageSize);
    }
}
